package com.example.maillist;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import item.Mail;
import service.mailService;

public class UpdateActivity extends AppCompatActivity {
    private Mail mail;
    private mailService mailservice;
    ArrayAdapter<CharSequence> adapter2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);

        mailservice = new mailService(this);
        Intent intent = getIntent();
        final int id = intent.getIntExtra("updateId",0);
        System.out.print("position:update"+id);
        mail = mailservice.query(id);

        Button btn = findViewById(R.id.goBack);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UpdateActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
        TextView text = findViewById(R.id.upName);
        TextView text2 = findViewById(R.id.upNumber);
        Spinner spin = (Spinner) findViewById(R.id.upclass);

        adapter2 = ArrayAdapter.createFromResource(this, R.array.grades, android.R.layout.simple_spinner_item);

        adapter2.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);

        spin.setAdapter(adapter2);
        spin.setVisibility(View.VISIBLE);
        int pos = adapter2.getPosition(mail.getGrade());
        spin.setSelection(pos);
        RadioButton male_rb = findViewById(R.id.radio1);
        RadioButton female_rb = findViewById(R.id.radio2);

        TextView text5 = findViewById(R.id.upemail);
        String gender = mail.getGender();
        switch (gender){
            case "男":
                male_rb.setChecked(true);
                break;
            case "女":
                female_rb.setChecked(true);
                break;
        }
        text.setText(mail.getName());
        text2.setText(mail.getNumber());

        text5.setText(mail.getEmail());

        Button btn2 = findViewById(R.id.update);
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView text = findViewById(R.id.upName);
                TextView text2 = findViewById(R.id.upNumber);
                Spinner text3 = findViewById(R.id.upclass);
                RadioGroup rg = findViewById(R.id.radioGroup);
                final String text4 = ((RadioButton)findViewById(rg.getCheckedRadioButtonId())).getText().toString();
                TextView text5 = findViewById(R.id.upemail);
                mail.setId((long) id);
                mail.setName(text.getText().toString());
                mail.setNumber(text2.getText().toString());
                mail.setGrade(text3.getSelectedItem().toString());
                mail.setEmail(text5.getText().toString());
                mail.setGender(text4);

                mailservice.update(mail);

                android.content.DialogInterface.OnClickListener listener =
                        new android.content.DialogInterface.OnClickListener(){
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(UpdateActivity.this,MainActivity.class);
                                startActivity(intent);
                            }
                        };
                AlertDialog.Builder builder = new AlertDialog.Builder(UpdateActivity.this);
                builder.setTitle("修改成功");
                builder.setPositiveButton("确定",listener);
                builder.show();

            }
        });

    }


}
